import configparser
import os
from typing import Dict, Any


class Config:
    """配置管理类，负责加载和管理配置文件"""
    
    def __init__(self, config_file: str = "config.ini"):
        self.config_file = config_file
        self.config = configparser.ConfigParser()
        self.load_config()
    
    def load_config(self):
        """加载配置文件"""
        if not os.path.exists(self.config_file):
            raise FileNotFoundError(f"配置文件 {self.config_file} 不存在")
        
        self.config.read(self.config_file, encoding='utf-8')
    
    def get_section(self, section: str) -> Dict[str, Any]:
        """获取指定配置节的所有配置"""
        if section not in self.config:
            raise ValueError(f"配置节 {section} 不存在")
        
        return dict(self.config[section])
    
    def get(self, section: str, key: str, fallback: Any = None) -> str:
        """获取指定配置项的值"""
        return self.config.get(section, key, fallback=fallback)
    
    def getint(self, section: str, key: str, fallback: int = None) -> int:
        """获取整数类型的配置项"""
        return self.config.getint(section, key, fallback=fallback)
    
    def getfloat(self, section: str, key: str, fallback: float = None) -> float:
        """获取浮点数类型的配置项"""
        return self.config.getfloat(section, key, fallback=fallback)
    
    def getboolean(self, section: str, key: str, fallback: bool = None) -> bool:
        """获取布尔类型的配置项"""
        return self.config.getboolean(section, key, fallback=fallback)
    
    @property
    def database_config(self) -> Dict[str, Any]:
        """获取数据库配置"""
        return self.get_section('database')
    
    @property
    def embedding_config(self) -> Dict[str, Any]:
        """获取嵌入模型配置"""
        return self.get_section('embedding')
    
    @property
    def llm_config(self) -> Dict[str, Any]:
        """获取大语言模型配置"""
        return self.get_section('llm')
    
    @property
    def rag_config(self) -> Dict[str, Any]:
        """获取RAG系统配置"""
        return self.get_section('rag')
    
    @property
    def logging_config(self) -> Dict[str, Any]:
        """获取日志配置"""
        return self.get_section('logging')


# 全局配置实例
config = Config()